<?php
// +----------------------------------------------------------------------
// | 友情链接管理
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: LinksAction.class.php 119 2011-05-13 08:05:33Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class LinksAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	//链接列表
	public function listLinks() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'link_sort';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		
		$links  =  M ('Links');
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );
		$count = $links->count();
		$p = new Page ($count, C('admin_list_num'));
		$list = $links->where('link_status = 1')->order("`".$order."` ".$sort)->limit($p->firstRow.','.$p->listRows)->findAll();
		$p->setConfig('theme',C('admin_page_theme'));
		$page = $p->show ();
		
		$this->assign('action_link', array('href'=>__URL__.'/addLinks/', 'text' => L('addLinks')));
		$this->assign('ur_here',L('listLinks'));
		$this->assign('pagestyle',C('admin_page_style'));	
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}

	//添加链接页面
	public function addLinks() {
		$this->assign('action_link', array('href'=>__URL__.'/listLinks/', 'text' => L('listLinks')));
		$this->assign('ur_here',L('addLinks'));
		$this->assign('default_order', 50);
		$this->display();
	}
	
	//添加链接
	public function insertLinks() {
		if ($_REQUEST['act']) {	
			$links = D('Links');
			if (false === $links->create()) {
				$this->error($links->getError());
			} else {
				if ($result = $links->add()) {
					clear_cache_files();    // 清除缓存
					set_log('add(Links, Id: '.$result.')');
					$this->assign('jumpUrl',__URL__.'/listLinks/');
					$this->success(L('success'));
				} else {
					$this->error(L('error'));
				}
			}
		} else {
			$this->error (L('illegal'));
		}	
	}

	//快速显示或隐藏友情链接
	public function visibleToggle() {
		$map	=	array();
		$map['link_id']	 =	$_REQUEST['id'];
		$v = ($_REQUEST['v'] == 0) ? 1 : 0;
		$links = M('Links');
		if ($list = $links->where($map)->setField('link_visible', $v)) {
			clear_cache_files();    // 清除缓存
			set_log('edit(Links, Toggle, Id: '.$_REQUEST['id'].')');
			$this->assign('jumpUrl',__URL__.'/listLinks/');
			$this->success(L('success'));
		} else {
			$this->error(L('error'));
		}
	}
	
	//快速更新情链接显示顺序
	public function sortLinks() {
		if ($_REQUEST['act']) {	
			$links = M('Links');
			$pk = $links->getPk();
			foreach($_POST['key'] as $key => $val) {
				$list = $links->where("$pk = $key")->setField('link_sort', $val);
			}
			if ($list !== false) {
				clear_cache_files();    // 清除缓存
				set_log('edit(Links, Sort)');
				$this->assign('jumpUrl',__URL__.'/listLinks/');
				$this->success (L('success'));
			} else {
				$this->error (L('error'));
			}
		} else {
			$this->error (L('illegal'));
		}	
	}
	
	//编辑链接页面
	public function editLinks() {
		$links	= M('Links');
		$vo	=	$links->getByLinkId($_REQUEST['id']);
		$this->assign('vo',$vo);
		$this->assign('action_link', array('href'=>__URL__.'/listLinks/', 'text' => L('listLinks')));
		$this->assign('ur_here',L('editLinks'));
		$this->display();
	}
	
	//编辑链接
	public function updateLinks() {
		$links = D('Links');
		if (false === $links->create()) {
			$this->error($links->getError());
		} else {
			$result = $links->save();
			if (false !== $result) {
				clear_cache_files();    // 清除缓存
				set_log('edit(Links, Id: '.$links->link_id.')');
				$this->assign('jumpUrl',__URL__.'/listLinks/');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		}
	}
	
	//扔进回收站
	public function trashLinks() {
		$map	=	array();
		$map['link_id']	 =	$_REQUEST['id'];
		$links = M('Links');
		if ($list = $links->where($map)->setField('link_status', 0)) {
			clear_cache_files();    // 清除缓存
			set_log('trash(Links, Id: '.$_REQUEST['id'].')');
			$this->assign('jumpUrl',__URL__.'/listLinks/');
			$this->success(L('success'));
		} else {
			$this->error(L('error'));
		}
	}
	
}

?>
